{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Target Indices After Sorting Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #binary-search #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #二分查找 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: targetIndices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出数组排序后的目标下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 以及一个目标元素 <code>target</code> 。</p>\n",
    "\n",
    "<p><strong>目标下标</strong> 是一个满足&nbsp;<code>nums[i] == target</code> 的下标 <code>i</code> 。</p>\n",
    "\n",
    "<p>将 <code>nums</code> 按 <strong>非递减</strong> 顺序排序后，返回由 <code>nums</code> 中目标下标组成的列表。如果不存在目标下标，返回一个 <strong>空</strong> 列表。返回的列表必须按 <strong>递增</strong> 顺序排列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,5,2,3], target = 2\n",
    "<strong>输出：</strong>[1,2]\n",
    "<strong>解释：</strong>排序后，nums 变为 [1,<em><strong>2</strong></em>,<em><strong>2</strong></em>,3,5] 。\n",
    "满足 nums[i] == 2 的下标是 1 和 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,5,2,3], target = 3\n",
    "<strong>输出：</strong>[3]\n",
    "<strong>解释：</strong>排序后，nums 变为 [1,2,2,<em><strong>3</strong></em>,5] 。\n",
    "满足 nums[i] == 3 的下标是 3 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,5,2,3], target = 5\n",
    "<strong>输出：</strong>[4]\n",
    "<strong>解释：</strong>排序后，nums 变为 [1,2,2,3,<em><strong>5</strong></em>] 。\n",
    "满足 nums[i] == 5 的下标是 4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,5,2,3], target = 4\n",
    "<strong>输出：</strong>[]\n",
    "<strong>解释：</strong>nums 中不含值为 4 的元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i], target &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-target-indices-after-sorting-array](https://leetcode.cn/problems/find-target-indices-after-sorting-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-target-indices-after-sorting-array](https://leetcode.cn/problems/find-target-indices-after-sorting-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,5,2,3]\\n2', '[1,2,5,2,3]\\n3', '[1,2,5,2,3]\\n5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        dic = {}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in dic:\n",
    "                dic[nums[i]] = [i]\n",
    "            else:\n",
    "                dic[nums[i]].append(i)\n",
    "        if target in dic:\n",
    "            return [j for j in range(dic[target][0], dic[target][-1] + 1)]\n",
    "        else:\n",
    "            return []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        return [i for i, n in enumerate(sorted(nums)) if n == target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        l = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                l.append(i)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        indexes = []\n",
    "        nums.sort()\n",
    "        for index, val in enumerate(nums):\n",
    "            if val == target:\n",
    "                indexes.append(index)\n",
    "        return indexes\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        res = []\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        nums.sort()\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        output = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                output.append(i)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        result = []\n",
    "        minum = 0\n",
    "        targetnum = 0\n",
    "        for val in nums :\n",
    "            if val == target:\n",
    "                targetnum = targetnum + 1\n",
    "            elif val < target:\n",
    "                minum = minum + 1\n",
    "        return [x + minum for x in range(targetnum)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        i = 0\n",
    "        ans =[]\n",
    "        for num in nums:\n",
    "            if nums[i] ==target:\n",
    "                ans.append(i)\n",
    "            i = i+1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                res.append(i)\n",
    "        #res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        ans = []\n",
    "        nums.sort()\n",
    "        for i, x in enumerate(nums):\n",
    "            if x==target:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i, each in enumerate(nums):\n",
    "            if each == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        return [i for i, num in enumerate(sorted(nums)) if num == target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        output = []\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == target:\n",
    "                output.append(i)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        list1 = []\n",
    "        for i in range(0,len(nums)):\n",
    "            if nums[i]==target:\n",
    "                list1.append(i)\n",
    "        return list1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        return [i for i in range(len(nums)) if nums[i] == target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        if target not in nums:\n",
    "            return []\n",
    "        res = []\n",
    "        for i, v in enumerate(sorted(nums)):\n",
    "            if v == target:\n",
    "                res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        dic = {}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in dic:\n",
    "                dic[nums[i]] = [i]\n",
    "            else:\n",
    "                dic[nums[i]].append(i)\n",
    "        return [j for j in range(dic[target][0], dic[target][-1] + 1)] if target in dic else []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                result.append(i)\n",
    "        return result\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        ans = []\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            elif nums[i] > target:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        return [i for i in range(len(nums)) if nums[i] == target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        if target not in nums:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            if nums[i] > target:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                res.append(i)\n",
    "            else:\n",
    "                if not res:\n",
    "                    continue\n",
    "                else:\n",
    "                    break\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: list[int], target: int) -> list[int]:\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res=[]\n",
    "        for i,v in enumerate(nums):\n",
    "            if v==target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        return [i for i in range(len(nums)) if sorted(nums)[i] == target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        less_cnt = 0\n",
    "        equal_cnt = 0\n",
    "        for x in nums:\n",
    "            if x < target:\n",
    "                less_cnt += 1\n",
    "            elif x == target:\n",
    "                equal_cnt += 1\n",
    "        \n",
    "        res = []\n",
    "        for i in range(equal_cnt):\n",
    "            res.append(less_cnt + i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        cnt1 = 0   # 小于 target 的元素数量\n",
    "        cnt2 = 0   # 等于 target 的元素数量\n",
    "        for num in nums:\n",
    "            if num < target:\n",
    "                cnt1 += 1\n",
    "            elif num == target:\n",
    "                cnt2 += 1\n",
    "        res = list(range(cnt1, cnt1 + cnt2))   # 下标数组\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        anw = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                anw.append(i)\n",
    "        return anw    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        list1= []\n",
    "        for i,n in enumerate(nums):\n",
    "            if n == target:\n",
    "                list1.append(i)\n",
    "        return list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "            #题解跟二分法没关系\n",
    "            nums = sorted(nums) \n",
    "            res = []          \n",
    "            for i in range(len(nums)):\n",
    "                \n",
    "                if nums[i] == target:\n",
    "                    res.append(i)\n",
    "                    #return res.append(i) 错误语法\n",
    "            return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        ans=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                ans.append(i)\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        ans=[]\n",
    "        nt=sorted(nums)\n",
    "        count=0\n",
    "        for i in nt:\n",
    "            if i==target:\n",
    "                ans.append(count)\n",
    "            count+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        return list(range(bisect.bisect_left(nums, target), bisect.bisect_right(nums, target)))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        result=[i for i in range(len(nums)) if nums[i]==target]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        result = []\n",
    "        minum = 0\n",
    "        targetnum = 0\n",
    "        for val in nums :\n",
    "            if val == target:\n",
    "                targetnum = targetnum + 1\n",
    "            elif val < target:\n",
    "                minum = minum + 1\n",
    "        return [x + minum for x in range(targetnum)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        def binary_equallarge(nums, target):\n",
    "            left = 0\n",
    "            right = len(nums)\n",
    "            while left < right:\n",
    "                mid = (left + right) // 2\n",
    "                if nums[mid] < target:\n",
    "                    left = mid + 1\n",
    "                else:\n",
    "                    right = mid\n",
    "            return left\n",
    "        def binary_large(nums, target):\n",
    "            left = 0\n",
    "            right = len(nums) \n",
    "            while left < right:\n",
    "                mid = (left + right) // 2\n",
    "                if nums[mid] <= target:\n",
    "                    left = mid + 1\n",
    "                else:\n",
    "                    right = mid\n",
    "            return left\n",
    "        nums.sort()\n",
    "        #print(binary_equallarge(nums, target))\n",
    "        #print(binary_large(nums, target))\n",
    "        return list(range(binary_equallarge(nums, target), binary_large(nums, target)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        def binary_equallarge(nums, target):\n",
    "            left = 0\n",
    "            right = len(nums)\n",
    "            while left < right:\n",
    "                mid = (left + right) // 2\n",
    "                if nums[mid] < target:\n",
    "                    left = mid + 1\n",
    "                else:\n",
    "                    right = mid\n",
    "            return left\n",
    "        def binary_large(nums, target):\n",
    "            left = 0\n",
    "            right = len(nums) \n",
    "            while left < right:\n",
    "                mid = (left + right) // 2\n",
    "                if nums[mid] <= target:\n",
    "                    left = mid + 1\n",
    "                else:\n",
    "                    right = mid\n",
    "            return left\n",
    "        nums.sort()\n",
    "        #print(binary_equallarge(nums, target))\n",
    "        #print(binary_large(nums, target))\n",
    "        return list(range(binary_equallarge(nums, target), binary_large(nums, target)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        count = 0\n",
    "        counts = 0\n",
    "        for i in nums:\n",
    "            if i<target:\n",
    "                count+=1\n",
    "            if i == target:\n",
    "                counts+=1\n",
    "        ans = []\n",
    "        for i in range(counts):\n",
    "            ans.append(count+i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        output = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                output.append(i)\n",
    "                \n",
    "        return output  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        indexes = []\n",
    "        for i in range(len(nums)-1, 0, -1):\n",
    "            max_num = 0\n",
    "            for j in range(1, i+1):\n",
    "                if nums[j] > nums[max_num]:\n",
    "                    max_num = j\n",
    "            nums[i], nums[max_num] =  nums[max_num], nums[i]\n",
    "\n",
    "        for index, val in enumerate(nums):\n",
    "            if val == target:\n",
    "                indexes.append(index)\n",
    "        return indexes\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        res = []\n",
    "        nums = sorted(nums)\n",
    "        print(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        nums.sort()\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            if nums[i] > target:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        x = 0   # 小于 target 的元素数量\n",
    "        y = 0   # 等于 target 的元素数量\n",
    "        for num in nums:\n",
    "            if num < target:\n",
    "                x += 1\n",
    "            elif num == target:\n",
    "                y += 1\n",
    "        res = list(range(x, x + y))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        return list(range(bisect.bisect_left(nums,target),bisect.bisect_right(nums,target)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        ans = []\n",
    "        nums = sorted(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        r = []\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == target:\n",
    "                r.append(i)\n",
    "            if n > target:\n",
    "                break\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        return list(filter(lambda x: x is not None, (idx if num == target else None for idx, num in enumerate(sorted(nums)))))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        if target not in nums:\n",
    "            return []\n",
    "        res = []\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        if target not in nums:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            if nums[i] > target:\n",
    "                return ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        lis = list()\n",
    "        nums.sort()\n",
    "        for x in range(len(nums)) :\n",
    "            if nums[x] == target :\n",
    "                lis.append(x)\n",
    "        return lis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        lst=[]\n",
    "        for i in range(len(nums)):\n",
    "            if  nums[i]==target:\n",
    "                lst.append(i)\n",
    "                nums[i]==target+1\n",
    "        return lst\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        if target not in nums:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            if nums[i] > target:\n",
    "                return ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                ans.append(i)\n",
    "            if nums[i] > target:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        ans=[]\n",
    "        nums.sort() \n",
    "        for i,num in enumerate(nums):\n",
    "            if target==num:\n",
    "                ans.append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        if target in nums:\n",
    "            for i in range(len(nums)):\n",
    "                for j in range(i,len(nums)):\n",
    "                    if nums[i]>nums[j]:\n",
    "                        nums[i],nums[j]=nums[j],nums[i]\n",
    "            re=[]\n",
    "            num=nums.count(target)\n",
    "            for n in range(nums.index(target),nums.index(target)+num):\n",
    "                re.append(n)\n",
    "            return re\n",
    "        return []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if nums[i]==target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        cnt1 = 0   # 小于 target 的元素数量\n",
    "        cnt2 = 0   # 等于 target 的元素数量\n",
    "        for num in nums:\n",
    "            if num < target:\n",
    "                cnt1 += 1\n",
    "            elif num == target:\n",
    "                cnt2 += 1\n",
    "        res = list(range(cnt1, cnt1 + cnt2))   # 下标数组\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        l = bisect_left(nums, target)\n",
    "        if l >= n or nums[l] != target:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(l, n):\n",
    "            if nums[i] != target:\n",
    "                break\n",
    "            ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        res=[]\n",
    "        for i in range(n):\n",
    "            if nums[i]==target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        res=[]\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        a = []\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i] == target:\n",
    "                a.append(i)\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        def binarySearch(i, j, target, nums):\n",
    "            while j - i > 3:\n",
    "                mid = (i + j) >> 1\n",
    "                if nums[mid] >= target: j = mid\n",
    "                else: i = mid + 1\n",
    "            for k in range(i, j+1):\n",
    "                if nums[k] >= target:\n",
    "                    return k\n",
    "            return j+1\n",
    "        nums.sort()\n",
    "        pos = binarySearch(0, len(nums)-1, target, nums)\n",
    "        res = []\n",
    "        for i in range(pos, len(nums)):\n",
    "            if nums[i] == target:\n",
    "                res.append(i)\n",
    "            else:\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        if (target not in nums):\n",
    "            return []\n",
    "\n",
    "        addNumber = 0\n",
    "        result = []\n",
    "        while target in nums:\n",
    "            result.append(nums.index(target) + addNumber)\n",
    "            nums.pop(nums.index(target))\n",
    "            addNumber += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        n = sorted(nums)\n",
    "        alist = []\n",
    "        for index in range(len(nums)):\n",
    "            if n[index]==target:\n",
    "                alist.append(index)\n",
    "        return alist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def targetIndices(self, nums: List[int], target: int) -> List[int]:\n",
    "        nums.sort()\n",
    "        l=[]\n",
    "        for i,num in enumerate(nums):\n",
    "            if num==target:\n",
    "                l.append(i)\n",
    "        return l"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
